import 'package:flutter/material.dart';
import 'package:flutter_app/utils/toast_utils.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_screenutil/src/size_extension.dart';
import 'package:get/get.dart';

import 'main_logic.dart';

/**
 * @Description: 主页面
 * @Author: 下雨了
 * @CreateDate: 2022年5月31日 13:59:55
 * @Version: 1.0
 */
///
class MainPage extends StatefulWidget {
  final String title;

  MainPage({Key? key, required this.title}) : super(key: key);

  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {

  final logic = Get.put(MainLogic());
  final state = Get.find<MainLogic>().state;

  /// 自定义 tabBar
  Widget _tabBar(int index) {
    TextStyle style = new TextStyle(fontSize: 11.sp, color: MyColors.FF333333);
    String imgUrl = logic.normalIcon[index];
    if (state.currentIndex == index) {
      style = TextStyle(fontSize: 11.sp, color: MyColors.FFE5261E);
      imgUrl = logic.selectedIcon[index];
    }
    late Widget tabBar;
    if (index == 1) {
      tabBar = Stack(
        alignment: Alignment.center,
        children: [
          new Container(
            child: GestureDetector(
              behavior: HitTestBehavior.opaque,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Container(
                      child: Image.asset(imgUrl, width: 22.r, height: 22.r),
                      margin: EdgeInsets.only(bottom: 1.r)),
                  Text(logic.titles[index], style: style)
                ],
              ),
              onTap: () {
                if (state.currentIndex != index) {
                  setState(() {
                    state.currentIndex = index;
                  });
                }
              },
            ),
          ),
          Positioned(
            top: 0.r,
            right: 35.r,
            child: Obx(() {
              return Visibility(
                visible: state.noticeReadCount.value != 0,
                child: Container(
                  width: 20.r,
                  padding: EdgeInsets.fromLTRB(0, 2.r, 0, 2.r),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      color: MyColors.FFE5261E,
                      shape: BoxShape.rectangle,
                      borderRadius: BorderRadius.all(Radius.circular(10.r))),
                  child: Text(state.noticeReadCount.value.toString(),
                      style: TextStyle(fontSize: 10.sp, color: Colors.white),),
                ),
              );
            }),
          )
        ],
      );
    } else {
      tabBar = new Container(
        child: GestureDetector(
          behavior: HitTestBehavior.opaque,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                  child: Image.asset(imgUrl, width: 22.r, height: 22.r),
                  margin: EdgeInsets.only(bottom: 1.r)),
              Text(logic.titles[index], style: style)
            ],
          ),
          onTap: () {
            if (state.currentIndex != index) {
              setState(() {
                state.currentIndex = index;
              });
            }
          },
        ),
      );
    }
    return tabBar;
  }

  DateTime? lastPopTime;
  @override
  Widget build(BuildContext context) {
    return GetBuilder<MainLogic>(builder: (logic) {
      return WillPopScope(
        onWillPop: () async {
          if (lastPopTime == null || DateTime.now().difference(lastPopTime!) > Duration(seconds: 1)) {
            lastPopTime = DateTime.now();
            toast.show("再按一次退出");
            return Future.value(false);
          } else {
            lastPopTime = DateTime.now();
            // 退出app
            return Future.value(true);
          }
        },
        child: Scaffold(
          backgroundColor: MyColors.FFF1F1F1,
          body: IndexedStack(index: state.currentIndex, children: logic.fragment),
          bottomNavigationBar: Container(
            padding: EdgeInsets.only(top: 7.5.r, bottom: 6.5.r),
            decoration: BoxDecoration(
                boxShadow: [BoxShadow(
                    color: Color(0x1a000000),
                    blurRadius: 6.67.r,
                    spreadRadius: 0.05.r,
                )],
                shape: BoxShape.rectangle,
                color: Colors.white
            ),
            child: SafeArea(
              child: Row(
                children: [
                  Expanded(child: _tabBar(0)),
                  Expanded(child: _tabBar(1)),
                  Expanded(child: _tabBar(2)),
                  // Expanded(child: _tabBar(3)),
                ],
              ),
            ),
          ),
        ),
      );
    });
  }

  @override
  void dispose() {
    Get.delete<MainLogic>();
    super.dispose();
  }
}
